//
//  WmcQRCodeUtil.swift
//  YPWatermarkCamera
//
//  Created by Json on 2023/7/29.
//

import UIKit

struct WmcQRCodeUtil {
    
    enum InputCorrection: String {
        case low      = "L"
        case medium   = "M"
        case quartile = "Q"
        case high     = "H"
    }
    
    static func createQRCodeImage(with content: String, inputCorrection: InputCorrection = .medium) -> UIImage? {
        guard let filter = CIFilter(name: "CIQRCodeGenerator") else {
            return nil
        }
        filter.setDefaults()
        let data = content.data(using: .utf8)
        filter.setValue(data, forKeyPath: "inputMessage")
        filter.setValue(inputCorrection.rawValue, forKeyPath: "inputCorrectionLevel")
        guard let outputImage = filter.outputImage else {
            WmcWarning("生成二维码失败")
            return nil
        }
        WmcDebug("二维码期望大小==>\(outputImage.extent.width):\(outputImage.extent.height)")
        // 缩放处理
        let scale = UIScreen.main.scale
        let scaleImage = outputImage.transformed(by: CGAffineTransform(scaleX: scale, y: scale))
        let context = CIContext(options: nil)
        guard let cgImage = context.createCGImage(scaleImage, from: scaleImage.extent) else {
            WmcWarning("二维码缩放处理失败")
            return nil }
        let image = UIImage(cgImage: cgImage, scale: scale, orientation: .up)
        WmcDebug("二维码实际大小==>\(image.size.width):\(image.size.height)")
        return image
    }
    
   
    
    
   

    
}
